import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainFrame extends JFrame {

    StudentManager stm = new StudentManager();

    public MainFrame() {

        // 设置窗口标题
        setTitle("班级管理系统");
        // 修改窗口大小为600x500
        setSize(600, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建顶部按钮面板
        JPanel topPanel = new JPanel();
        // 减小水平间距为20
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
        topPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));

        // 创建班级管理按钮和对应的弹出菜单
        JButton classBtn = createStyledButton("班级管理");
        JPopupMenu classMenu = createPopupMenu("增加班级", "班级列表");

        // 获取"增加班级"菜单项并添加点击事件
        JMenuItem addClass = (JMenuItem) classMenu.getComponent(0);
        addClass.addActionListener(e -> {
            stm.addNewClass(); // 调StudentManager的addClass方法
            JOptionPane.showMessageDialog(this, "增加班级成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
        });

        // 获取"班级列表"菜单项并添加点击事件
        JMenuItem listClass = (JMenuItem) classMenu.getComponent(1);
        listClass.addActionListener(e -> showClassListDialog());

        setupPopupMenu(classBtn, classMenu);

        // 创建小组管理的菜单
        JButton groupBtn = createStyledButton("小组管理");
        JPopupMenu groupMenu = createPopupMenu("增加小组", "小组列表");

        // 获取"增加小组"菜单项并添加点击事件
        JMenuItem addGroup = (JMenuItem) groupMenu.getComponent(0);
        addGroup.addActionListener(e -> showAddGroupDialog());

        // 获取"小组列表"菜单项并添加点击事件
        JMenuItem listGroup = (JMenuItem) groupMenu.getComponent(1);
        listGroup.addActionListener(e -> showGroupListDialog());

        setupPopupMenu(groupBtn, groupMenu);

        // 创建学生管理的菜单
        JButton studentBtn = createStyledButton("学生管理");
        JPopupMenu studentMenu = createPopupMenu("增加学生", "学生列表");

        // 获取"增加学生"菜单项并添加点击事件
        JMenuItem addStudent = (JMenuItem) studentMenu.getComponent(0);
        addStudent.addActionListener(e -> showAddStudentDialog());

        // 获取"学生列表"菜单项并添加点击事件
        JMenuItem listStudent = (JMenuItem) studentMenu.getComponent(1);
        listStudent.addActionListener(e -> showStudentListDialog());

        setupPopupMenu(studentBtn, studentMenu);

        // 创建课堂管理的菜单
        JButton courseBtn = createStyledButton("课堂管理");
        JPopupMenu courseMenu = new JPopupMenu();

        // 创建四个菜单项
        String[] menuItems = {
                "随机抽取学生",

                "记录学生成绩",
                "查看成绩"
        };

        // 添加菜单项并设置点击事件
        for (String item : menuItems) {
            JMenuItem menuItem = new JMenuItem(item);
            menuItem.setFont(new Font("宋体", Font.PLAIN, 12));
            if (item.equals("随机抽取学生")) {
                menuItem.addActionListener(e -> showRandomStudent());
            } else if (item.equals("记录学生成绩")) {
                menuItem.addActionListener(e -> showRecordGradeDialog());
            } else if (item.equals("查看成绩")) {
                menuItem.addActionListener(e -> showViewGradesDialog());
            }
            courseMenu.add(menuItem);
        }

        // 设置菜单的显示和隐藏
        setupPopupMenu(courseBtn, courseMenu);

        // 将按钮添加到顶部面板
        topPanel.add(classBtn);
        topPanel.add(groupBtn);
        topPanel.add(studentBtn);
        topPanel.add(courseBtn);

        // 修改中央显示面板
        JPanel centerPanel = new JPanel();
        // 使用 GridBagLayout 来实现完全居中
        centerPanel.setLayout(new GridBagLayout());

        JLabel titleLabel = new JLabel("班级管理系统");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 48));

        // 将标签添加到中央面板
        centerPanel.add(titleLabel);

        // 设置布局
        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
    }

    // 创建一个统一样式的按钮
    private JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        // 减小按钮尺寸为100x30
        button.setPreferredSize(new Dimension(100, 30));
        button.setFont(new Font("宋体", Font.PLAIN, 14));
        return button;
    }

    // 创建弹出菜单的辅助方法
    private JPopupMenu createPopupMenu(String item1, String item2) {
        JPopupMenu menu = new JPopupMenu();
        Font menuFont = new Font("宋体", Font.PLAIN, 12);

        JMenuItem menuItem1 = new JMenuItem(item1);
        JMenuItem menuItem2 = new JMenuItem(item2);

        menuItem1.setFont(menuFont);
        menuItem2.setFont(menuFont);

        menu.add(menuItem1);
        menu.add(menuItem2);

        return menu;
    }

    // 设置弹出菜单的显示和隐藏
    private void setupPopupMenu(JButton button, JPopupMenu menu) {
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                menu.show(button, 0, button.getHeight());
            }
        });

        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                Point p = e.getPoint();
                Rectangle bounds = menu.getBounds();
                if (!bounds.contains(p)) {
                    menu.setVisible(false);
                }
            }
        });
    }

    // 显示添加学生的对话框
    private void showAddStudentDialog() {
        // 创建对话框
        JDialog dialog = new JDialog(this, "增加学生", true);
        dialog.setLayout(new BorderLayout());

        // 创建输入板
        JPanel inputPanel = new JPanel(new GridBagLayout());
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);

        // 学生姓名输入
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(new JLabel("学生姓名:"), gbc);
        gbc.gridx = 1;
        JTextField nameField = new JTextField(20);
        inputPanel.add(nameField, gbc);

        // 学号输入
        gbc.gridx = 0;
        gbc.gridy = 1;
        inputPanel.add(new JLabel("学    号:"), gbc);
        gbc.gridx = 1;
        JTextField idField = new JTextField(20);
        inputPanel.add(idField, gbc);

        // 班级选择
        gbc.gridx = 0;
        gbc.gridy = 2;
        inputPanel.add(new JLabel("所在班级:"), gbc);
        gbc.gridx = 1;
        JComboBox<String> classCombo = new JComboBox<>(stm.getAllClasses());
        inputPanel.add(classCombo, gbc);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        JButton confirmBtn = new JButton("确定");
        JButton cancelBtn = new JButton("取消");

        // 按钮事件保持不变
        confirmBtn.addActionListener(e -> {
            String name = nameField.getText().trim();
            int id = Integer.parseInt(idField.getText().trim());
            String temp = (String) classCombo.getSelectedItem();
            int className = Integer.parseInt(String.valueOf(temp.charAt(temp.length() - 1)));

            if (name.isEmpty()) {
                JOptionPane.showMessageDialog(dialog, "请填写完整信息！", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            // TODO: 这里添加保存学生信息的代码
            stm.addStudent(name, id, className);

            JOptionPane.showMessageDialog(dialog, "学生添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
            dialog.dispose();
        });

        cancelBtn.addActionListener(e -> dialog.dispose());

        buttonPanel.add(confirmBtn);
        buttonPanel.add(cancelBtn);

        // 使用 BorderLayout 添加面板
        dialog.add(inputPanel, BorderLayout.CENTER);
        dialog.add(buttonPanel, BorderLayout.SOUTH);

        // 设置对话框属性
        dialog.setSize(350, 200);
        dialog.setLocationRelativeTo(this);
        dialog.setResizable(false);
        dialog.setVisible(true);
    }

    // 显示学生列表对话框
    private void showStudentListDialog() {
        // 创建输入班级的对话框
        JDialog inputDialog = new JDialog(this, "请输入班级", true);
        inputDialog.setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        inputPanel.add(new JLabel("班级编号:"));
        JComboBox<String> classCombo = new JComboBox<>(stm.getAllClasses());
        inputPanel.add(classCombo);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton confirmBtn = new JButton("确定");

        confirmBtn.addActionListener(e -> {
            inputDialog.dispose();
            String selectedClass = (String) classCombo.getSelectedItem();
            int classNum = Integer.parseInt(String.valueOf(selectedClass.charAt(selectedClass.length() - 1)));
            showStudentTable(classNum);
        });

        buttonPanel.add(confirmBtn);

        inputDialog.add(inputPanel, BorderLayout.CENTER);
        inputDialog.add(buttonPanel, BorderLayout.SOUTH);
        inputDialog.setSize(250, 150);
        inputDialog.setLocationRelativeTo(this);
        inputDialog.setResizable(false);
        inputDialog.setVisible(true);
    }

    // 显示学生表格
    private void showStudentTable(int classNum) {
        // 创建表格对话框
        JDialog tableDialog = new JDialog(this, "班级" + classNum + "学生列表", true);
        tableDialog.setLayout(new BorderLayout());

        // 创建表格模型
        String[] columnNames = { "学生姓名", "学号" };
        Object[][] data = stm.listStudents(classNum); // 从StudentManager获取数据

        JTable table = new JTable(data, columnNames);
        table.setFillsViewportHeight(true);

        // 添加滚动面板
        JScrollPane scrollPane = new JScrollPane(table);
        tableDialog.add(scrollPane, BorderLayout.CENTER);

        // 设置对话框属性
        tableDialog.setSize(400, 300);
        tableDialog.setLocationRelativeTo(this);
        tableDialog.setVisible(true);
    }

    // 显示班级列表对话框
    private void showClassListDialog() {
        // 创建表格对话框
        JDialog dialog = new JDialog(this, "班级列表", true);
        dialog.setLayout(new BorderLayout());

        // 创建表格模型
        String[] columnNames = { "班级", "人数" };
        Object[][] data = stm.listClasses(); // 从StudentManager获取数据

        JTable table = new JTable(data, columnNames);
        table.setFillsViewportHeight(true);

        // 添加滚动面板
        JScrollPane scrollPane = new JScrollPane(table);
        dialog.add(scrollPane, BorderLayout.CENTER);

        // 设置对话框属性
        dialog.setSize(300, 200);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    private void showAddGroupDialog() {
        JDialog dialog = new JDialog(this, "增加小组", true);
        dialog.setLayout(new BorderLayout());

        // 创建输入面板
        JPanel inputPanel = new JPanel(new GridBagLayout());
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);

        // 第一位学生信息
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(new JLabel("第一位学生姓名:"), gbc);
        gbc.gridx = 1;
        JTextField name1Field = new JTextField(20);
        inputPanel.add(name1Field, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        inputPanel.add(new JLabel("第一位学生学号:"), gbc);
        gbc.gridx = 1;
        JTextField id1Field = new JTextField(20);
        inputPanel.add(id1Field, gbc);

        // 第二位学生信息
        gbc.gridx = 0;
        gbc.gridy = 2;
        inputPanel.add(new JLabel("第二位学生姓名:"), gbc);
        gbc.gridx = 1;
        JTextField name2Field = new JTextField(20);
        inputPanel.add(name2Field, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        inputPanel.add(new JLabel("第二位学生学号:"), gbc);
        gbc.gridx = 1;
        JTextField id2Field = new JTextField(20);
        inputPanel.add(id2Field, gbc);

        // 班级选择
        gbc.gridx = 0;
        gbc.gridy = 4;
        inputPanel.add(new JLabel("所在班级:"), gbc);
        gbc.gridx = 1;
        JComboBox<String> classCombo = new JComboBox<>(stm.getAllClasses());
        inputPanel.add(classCombo, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        JButton confirmBtn = new JButton("确定");
        JButton cancelBtn = new JButton("取消");

        confirmBtn.addActionListener(e -> {
            String name1 = name1Field.getText().trim();
            String name2 = name2Field.getText().trim();
            int id1 = Integer.parseInt(id1Field.getText().trim());
            int id2 = Integer.parseInt(id2Field.getText().trim());
            String temp = (String) classCombo.getSelectedItem();
            int className = Integer.parseInt(String.valueOf(temp.charAt(temp.length() - 1)));

            if (name1.isEmpty() || name2.isEmpty()) {
                JOptionPane.showMessageDialog(dialog, "请填写完整信息！", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            stm.addNewGroup(name1, id1, name2, id2, className);
            JOptionPane.showMessageDialog(dialog, "小组添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
            dialog.dispose();
        });

        cancelBtn.addActionListener(e -> dialog.dispose());

        buttonPanel.add(confirmBtn);
        buttonPanel.add(cancelBtn);

        dialog.add(inputPanel, BorderLayout.CENTER);
        dialog.add(buttonPanel, BorderLayout.SOUTH);

        dialog.setSize(400, 300);
        dialog.setLocationRelativeTo(this);
        dialog.setResizable(false);
        dialog.setVisible(true);
    }

    private void showGroupListDialog() {
        // 创建输入班级的对话框
        JDialog inputDialog = new JDialog(this, "请选择班级", true);
        inputDialog.setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        inputPanel.add(new JLabel("班级:"));
        JComboBox<String> classCombo = new JComboBox<>(stm.getAllClasses());
        inputPanel.add(classCombo);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton confirmBtn = new JButton("确定");

        confirmBtn.addActionListener(e -> {
            inputDialog.dispose();
            String selectedClass = (String) classCombo.getSelectedItem();
            int classNum = Integer.parseInt(String.valueOf(selectedClass.charAt(selectedClass.length() - 1)));
            showGroupTable(classNum);
        });

        buttonPanel.add(confirmBtn);

        inputDialog.add(inputPanel, BorderLayout.CENTER);
        inputDialog.add(buttonPanel, BorderLayout.SOUTH);
        inputDialog.setSize(250, 150);
        inputDialog.setLocationRelativeTo(this);
        inputDialog.setResizable(false);
        inputDialog.setVisible(true);
    }

    private void showGroupTable(int classNum) {
        // 创建表格对话框
        JDialog tableDialog = new JDialog(this, "班级" + classNum + "小组列表", true);
        tableDialog.setLayout(new BorderLayout());

        // 创建表格模型
        String[] columnNames = { "小组", "学生1姓名", "学生1学号", "学生2姓名", "学生2学号" };
        Object[][] data = stm.listGroups(classNum); // 从StudentManager获取数据

        JTable table = new JTable(data, columnNames);
        table.setFillsViewportHeight(true);

        // 添加滚动面板
        JScrollPane scrollPane = new JScrollPane(table);
        tableDialog.add(scrollPane, BorderLayout.CENTER);

        // 设置对话框属性
        tableDialog.setSize(600, 400);
        tableDialog.setLocationRelativeTo(this);
        tableDialog.setVisible(true);
    }

    private void showRandomStudent() {
        // 创建选择班级的对话框
        JDialog inputDialog = new JDialog(this, "请选择班级", true);
        inputDialog.setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        inputPanel.add(new JLabel("班级:"));
        JComboBox<String> classCombo = new JComboBox<>(stm.getAllClasses());
        inputPanel.add(classCombo);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton confirmBtn = new JButton("确定");

        confirmBtn.addActionListener(e -> {
            inputDialog.dispose();
            String selectedClass = (String) classCombo.getSelectedItem();
            int classNum = Integer.parseInt(String.valueOf(selectedClass.charAt(selectedClass.length() - 1)));

            // 从StudentManager获取随机学生信息
            Object[] student = stm.randomChooseStudent(classNum);
            if (student != null) {
                String message = String.format("姓名：%s\n学号：%s\n\n你被抽中了！",
                        student[0], student[1]);
                JOptionPane.showMessageDialog(this, message,
                        "随机抽取结果", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "该班级没有可供抽取的学生！",
                        "提示", JOptionPane.WARNING_MESSAGE);
            }
        });

        buttonPanel.add(confirmBtn);

        inputDialog.add(inputPanel, BorderLayout.CENTER);
        inputDialog.add(buttonPanel, BorderLayout.SOUTH);
        inputDialog.setSize(250, 150);
        inputDialog.setLocationRelativeTo(this);
        inputDialog.setResizable(false);
        inputDialog.setVisible(true);
    }

    private void showRecordGradeDialog() {
        // 创建选择班级的对话框
        JDialog dialog = new JDialog(this, "记录学生成绩", true);
        dialog.setLayout(new BorderLayout());

        // 创建输入面板
        JPanel inputPanel = new JPanel(new GridBagLayout());
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);

        // 班级选择
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(new JLabel("所在班级:"), gbc);
        gbc.gridx = 1;
        JComboBox<String> classCombo = new JComboBox<>(stm.getAllClasses());
        inputPanel.add(classCombo, gbc);

        // 学生姓名
        gbc.gridx = 0;
        gbc.gridy = 1;
        inputPanel.add(new JLabel("学生姓名:"), gbc);
        gbc.gridx = 1;
        JTextField nameField = new JTextField(20);
        inputPanel.add(nameField, gbc);

        // 学号
        gbc.gridx = 0;
        gbc.gridy = 2;
        inputPanel.add(new JLabel("学    号:"), gbc);
        gbc.gridx = 1;
        JTextField idField = new JTextField(20);
        inputPanel.add(idField, gbc);

        // 成绩
        gbc.gridx = 0;
        gbc.gridy = 3;
        inputPanel.add(new JLabel("成    绩:"), gbc);
        gbc.gridx = 1;
        JTextField gradeField = new JTextField(20);
        inputPanel.add(gradeField, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        JButton confirmBtn = new JButton("确定");
        JButton cancelBtn = new JButton("取消");

        confirmBtn.addActionListener(e -> {
            String name = nameField.getText().trim();
            String temp = (String) classCombo.getSelectedItem();
            int className = Integer.parseInt(String.valueOf(temp.charAt(temp.length() - 1)));

            try {
                int id = Integer.parseInt(idField.getText().trim());
                double grade = Double.parseDouble(gradeField.getText().trim());

                if (name.isEmpty()) {
                    JOptionPane.showMessageDialog(dialog, "请填写完整信息！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                stm.recordGrade(className, name, id, grade);
                JOptionPane.showMessageDialog(dialog, "成绩记录成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                dialog.dispose();
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(dialog, "请输入有效的学号和成绩！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

        cancelBtn.addActionListener(e -> dialog.dispose());

        buttonPanel.add(confirmBtn);
        buttonPanel.add(cancelBtn);

        dialog.add(inputPanel, BorderLayout.CENTER);
        dialog.add(buttonPanel, BorderLayout.SOUTH);

        dialog.setSize(350, 250);
        dialog.setLocationRelativeTo(this);
        dialog.setResizable(false);
        dialog.setVisible(true);
    }

    private void showViewGradesDialog() {
        // 创建选择班级的对话框
        JDialog inputDialog = new JDialog(this, "请选择班级", true);
        inputDialog.setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        inputPanel.add(new JLabel("班级:"));
        JComboBox<String> classCombo = new JComboBox<>(stm.getAllClasses());
        inputPanel.add(classCombo);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton confirmBtn = new JButton("确定");

        confirmBtn.addActionListener(e -> {
            inputDialog.dispose();
            String selectedClass = (String) classCombo.getSelectedItem();
            int classNum = Integer.parseInt(String.valueOf(selectedClass.charAt(selectedClass.length() - 1)));
            showGradesTable(classNum);
        });

        buttonPanel.add(confirmBtn);

        inputDialog.add(inputPanel, BorderLayout.CENTER);
        inputDialog.add(buttonPanel, BorderLayout.SOUTH);
        inputDialog.setSize(250, 150);
        inputDialog.setLocationRelativeTo(this);
        inputDialog.setResizable(false);
        inputDialog.setVisible(true);
    }

    private void showGradesTable(int classNum) {
        // 创建表格对话框
        JDialog tableDialog = new JDialog(this, "班级" + classNum + "成绩列表", true);
        tableDialog.setLayout(new BorderLayout());

        // 创建表格模型
        String[] columnNames = {"学号", "姓名", "成绩"};
        Object[][] data = stm.getGradesList(classNum); // 从StudentManager获取数据

        JTable table = new JTable(data, columnNames);
        table.setFillsViewportHeight(true);

        // 添加滚动面板
        JScrollPane scrollPane = new JScrollPane(table);
        tableDialog.add(scrollPane, BorderLayout.CENTER);

        // 设置对话框属性
        tableDialog.setSize(400, 300);
        tableDialog.setLocationRelativeTo(this);
        tableDialog.setVisible(true);
    }

    public static void main(String[] args) {
        // 使用SwingUtilities确保在EDT线程中运行
        SwingUtilities.invokeLater(() -> {
            MainFrame frame = new MainFrame();
            frame.setVisible(true);
        });
    }
}
